package com.atguigu.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @program: IDEAcode
 * @description:list集合的二分搜索法
 * @author: Mr.zjx
 * create: 2021-08-03-14-32
 **/
public class CollectionsTest {
    public static void main(String[] args) {
        listBinarySeach();
        listsort();
        listReverseOrder();
    }
    private static void listBinarySeach(){
        List<Integer> list = new ArrayList<Integer>();
        list.add(2);
        list.add(5);
        list.add(7);
        list.add(12);
        list.add(16);
        list.add(25);
        int index = Collections.binarySearch(list, 8);//找不到元素，返回插入点-1
        System.out.println("index = " + index);
        //list集合翻转
        Collections.reverse(list);
        System.out.println("list = " + list);
    }
    private static void listsort(){
        List<Integer> list = new ArrayList<Integer>();
        list.add(26);
        list.add(55);
        list.add(17);
        list.add(12);
        list.add(36);
        list.add(251);
        Collections.sort(list);//list集合的升序排列
        System.out.println("list = " + list);
    }

    private static void listReverseOrder(){
        //Collections工具类方法，获取比较器
        Comparator comparator = Collections.reverseOrder();
        List<Integer> list = new ArrayList<Integer>();
        list.add(26);
        list.add(55);
        list.add(17);
        list.add(12);
        list.add(36);
        list.add(251);
        //list集合sort先升序排列，传递比较器后降序排列
        Collections.sort(list,comparator);
        System.out.println("list = " + list);
    }
}
